package appapi.common;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * 统一返回
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/5/17 17:48
 */
public class ResponseBean<T> implements Serializable {

    private int code;

    private String message;

    private int rcount;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private TotalAmountBean totalAmount;

    /**
     * 该注解是不参与序列化，为null的时候不传递给前台
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public ResponseBean() {

    }

    public ResponseBean(int code, String message, TotalAmountBean totalAmount, T data) {
        setCode(code);
        setMessage(message);
        setTotalAmount(totalAmount);
        setData(data);
    }

    public ResponseBean(int code, String message, T data) {
        setCode(code);
        setMessage(message);
        setData(data);
    }

    public ResponseBean(int code, String message) {
        setCode(code);
        setMessage(message);
    }

    public ResponseBean(ResponseCodeEnum code) {
        setCode(code.getCode());
        setMessage(code.getMessage());
    }

    public ResponseBean(ResponseCodeEnum code, T data) {
        setCode(code.getCode());
        setMessage(code.getMessage());
        setData(data);
    }

    public ResponseBean(ResponseCodeEnum code, TotalAmountBean totalAmount, T data) {
        setCode(code.getCode());
        setMessage(code.getMessage());
        setTotalAmount(totalAmount);
        setData(data);
    }

    public ResponseBean(ResponseCodeEnum code, int size, T data) {
        setCode(code.getCode());
        setMessage(code.getMessage());
        setSize(size);
        setData(data);
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static ResponseBean success() {
        return new ResponseBean(ResponseCodeEnum.SUCCESS);
    }

    /**
     * 带提示信息的成功
     *
     * @return
     */
    public static ResponseBean success(String message) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS.getCode(), message);
    }

    /**
     * 操作成功（带返回数据和统计）
     *
     * @param data 需要返回的数据
     * @return
     */
    public static <T> ResponseBean success(TotalAmountBean totalAmount, T data) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS, totalAmount, data);
    }

    /**
     * 操作成功（带返回数据和统计）
     *
     * @param data 需要返回的数据
     * @return
     */
    public static <T> ResponseBean success(Integer size, T data) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS, size, data);
    }

    /**
     * 操作成功（带返回数据）
     *
     * @param data 需要返回的数据
     * @return
     */
    public static <T> ResponseBean success(T data) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS, data);
    }

    /**
     * 添加成功
     *
     * @return
     */
    public static ResponseBean addSuccess() {
        return new ResponseBean(ResponseCodeEnum.SUCCESS_ADD);
    }

    /**
     * 添加成功（带返回数据）
     *
     * @param data 需要返回的数据
     * @return
     */
    public static <T> ResponseBean addSuccess(T data) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS_ADD, data);
    }

    /**
     * 更新成功
     *
     * @return
     */
    public static ResponseBean updateSuccess() {
        return new ResponseBean(ResponseCodeEnum.SUCCESS_UPDATE);
    }

    /**
     * 更新成功（带返回数据）
     *
     * @param data 需要返回的数据
     * @return
     */
    public static <T> ResponseBean updateSuccess(T data) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS_UPDATE, data);
    }

    /**
     * 删除成功
     *
     * @return
     */
    public static ResponseBean deleteSuccess() {
        return new ResponseBean(ResponseCodeEnum.SUCCESS_DELETE);
    }

    public static <T> ResponseBean deleteSuccess(T data) {
        return new ResponseBean(ResponseCodeEnum.SUCCESS_DELETE, data);
    }

    /**
     * 失败
     *
     * @return
     */
    public static ResponseBean fail() {
        return new ResponseBean(ResponseCodeEnum.Fail);
    }

    /**
     * 失败
     *
     * @return
     */
    public static ResponseBean fail(String message) {
        return new ResponseBean(ResponseCodeEnum.Fail.getCode(), message);
    }

    /**
     * 失败
     *
     * @param code
     * @return
     */
    public static ResponseBean fail(ResponseCodeEnum code) {
        return new ResponseBean(code.getCode(), code.getMessage());
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @return
     */
    public static ResponseBean fail(ResponseCodeEnum code, String message) {
        return new ResponseBean(code.getCode(), message);
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @return
     */
    public static ResponseBean fail(int code, String message) {
        return new ResponseBean(code, message);
    }

    /**
     * 添加失败
     *
     * @return
     */
    public static ResponseBean addFail() {
        return new ResponseBean(ResponseCodeEnum.Fail_ADD);
    }

    /**
     * 更新失败
     *
     * @return
     */
    public static ResponseBean updateFail() {
        return new ResponseBean(ResponseCodeEnum.Fail_UPDATE);
    }

    /**
     * 删除失败
     *
     * @return
     */
    public static ResponseBean deleteFail() {
        return new ResponseBean(ResponseCodeEnum.Fail_DELETE);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public TotalAmountBean getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(TotalAmountBean totalAmount) {
        this.totalAmount = totalAmount;
    }

    public int getSize() {
        return rcount;
    }

    public void setSize(int size) {
        this.rcount = size;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
